import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gourmet/ui/custom_tabbar.dart';
import 'package:gourmet/utils/colors.dart';
import 'package:gourmet/utils/styles.dart';

class ToolsUtils {
  ToolsUtils._();

  static TextEditingController editingController(String content) {
    TextEditingController textEditingController =
        TextEditingController(text: content);
    textEditingController.value = TextEditingValue(
        text: content,
        selection: TextSelection.fromPosition(TextPosition(
            affinity: TextAffinity.downstream, offset: content.length)));
    return textEditingController;
  }

  static ClientTabBar getClientTabBar(
      List<String> tabs, TabController controller,
      {Color backgroundColor = Colors.transparent,
      ValueChanged<int>? onTap,
      TabBarIndicatorSize indicatorSize = TabBarIndicatorSize.label,
      bool isScrollable = true,
      double indicatorMinWidth = 36,
      double indicatorWeight = 2,
      String indicatorColor = ColorsUtils.colorFf64,
      String labelColor = ColorsUtils.colorFf64,
      TextStyle? labelStyle,
      String unselectedLabelColor = ColorsUtils.color6666,
      TextStyle? unselectedLabelStyle,
      List<Tab>? tabList}) {
    return ClientTabBar(
      tabs: tabList ??
          tabs.map((e) {
            return Tab(
              text: e,
            );
          }).toList(),
      controller: controller,
      indicatorSize: indicatorSize,
      isScrollable: isScrollable,
      padding: EdgeInsets.zero,
      indicator: RRecTabIndicator(
          radius: 1,
          insets: EdgeInsets.only(bottom: 5.w),
          color: ColorsUtils.parseColorFromHexString(indicatorColor)),
      indicatorMinWidth: indicatorMinWidth,
      indicatorWeight: indicatorWeight,
      labelColor: ColorsUtils.parseColorFromHexString(labelColor),
      labelStyle: labelStyle ??
          StylesUtils.customTextStyle(fontSize: StylesUtils.defaultFontSize),
      unselectedLabelColor:
          ColorsUtils.parseColorFromHexString(unselectedLabelColor),
      unselectedLabelStyle: unselectedLabelStyle ??
          StylesUtils.customTextStyle(fontSize: StylesUtils.defaultFontSize),
      backgroundColor: backgroundColor,
      onTap: onTap,
    );
  }

  static double calculateTextHeight(
      {String value = '',
      double maxWidth = double.infinity,
      required TextStyle style}) {
    if (value.isEmpty) {
      return 0.0;
    }
    TextPainter painter = TextPainter(
      locale: WidgetsBinding.instance.platformDispatcher.locale,
      textDirection: TextDirection.ltr,
      text: TextSpan(
        text: value,
        style: style,
      ),
    );
    painter.layout(maxWidth: maxWidth);
    return painter.height;
  }

  static double calculateTextWidth(
      {required TextStyle style,
      String value = '',
      maxWidth = double.infinity}) {
    if (value.isEmpty) {
      return 0.0;
    }
    TextPainter painter = TextPainter(
      locale: WidgetsBinding.instance.platformDispatcher.locale,
      textDirection: TextDirection.ltr,
      text: TextSpan(text: value, style: style),
    );
    painter.layout(maxWidth: maxWidth);
    return painter.width;
  }

  static AssetImage assetImageIcon(String assetName) {
    return AssetImage('assets/images/icon/$assetName');
  }

  static Image image(
    String assetName, {
    BoxFit? fit,
    double? width,
    double? height,
  }) {
    return Image.asset(
      'assets/images/$assetName',
      fit: fit,
      width: width,
      height: height,
    );
  }

  static AssetImage assetImage(String assetName) {
    return AssetImage(
      'assets/images/$assetName',
    );
  }

  static String getImgPath(String name, {String format = 'png'}) {
    return 'assets/images/$name.$format';
  }
}
